package cn.gs.reflect;

// @author gs
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MethodChooser {

    public static Class<?>[] getAllClass(Object... objs) {
        Class<?>[] ret = new Class<?>[objs.length];
        for (int i = 0; i < objs.length; ++i) {
            if (objs[i] == null) {
                ret[i] = null;
            } else {
                ret[i] = objs[i].getClass();
            }
        }
        return ret;
    }

    public static <T> T newIntance(Class<T> clazz, Object... args) {
        Class<?>[] types = getAllClass(args);
        try {
            Constructor<T> cont = clazz.getConstructor(types);
            T t = cont.newInstance(args);
            return t;
        } catch (Throwable ex) {
            Logger.getLogger(MethodChooser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
